<?php
/*
 * 手机客户端用户-注册接口
 * 2014-06-04
 */

include "../inc/public_header.php";
include "../func/user.php";				//用户信息处理函数

//常量
$mobile 	= $_REQUEST["mobile"];		
$userpwd 	= $_REQUEST["userpwd"];
$vcode		= $_REQUEST["vcode"];	
$imsi 		= $_REQUEST["imsi"];	
	

//注册
$success = 0;
$rs=array("success"=>"0");
$m_auth = 0;
if (isset($mobile) and isset($userpwd)){
	//判断手机号是否注册
	$sql = "select uid,uuid from user_base where mobile = '".$mobile."'";
	$db->query($sql);
	if($db->next_record()){
		$checkmobile_uid = $db->f("uid");
		$checkmobile_uuid = $db->f("uuid");
	}
	if (!$checkmobile_uid){
		//要求输入验证码
		if (isset($vcode)){
			//验证码输入
			$sql = "select vcode,counts from user_regvcode where mobile = '".$mobile."' and cuts > UNIX_TIMESTAMP()-1800";
			$db->query($sql);
			if($db->next_record()){
				$checkvcodecuts_vcode = $db->f("vcode");
				$checkvcodecuts_counts = $db->f("counts");
			}
			if (!$checkvcodecuts_vcode){
				//验证码不存在或已经过期
				$success = 2;
				$rs=array("success"=>"0","errCode"=>"0");
			}elseif ($vcode!=$checkvcodecuts_vcode){
				if($checkvcodecuts_counts>=3){
					$success = 3;
					$rs=array("success"=>"0","errCode"=>"0");
					$sql = " delete from user_regvcode where mobile = '".$mobile."'";
					$db->query($sql);
				}else{
					$sql = "update user_regvcode set counts = counts + 1 where mobile = '".$mobile."'";
					$db->query($sql);
					$success = 4;
					$rs=array("success"=>"0","errCode"=>"0");
				}
			}else{
				$success = 1;
			}
		}
		if ($success == 1 or !isset($vcode)){
			if (isset($vcode)){
				$m_auth = 1;
			}				
			$sql = "select uuid() as uuid,uuid() as muuid";
			$db->query($sql);
			if($db->next_record()){
				$uuid = $db->f("uuid");
				$muuid = $db->f("muuid");
			}
			//正式注册
			$sql = "insert into user_base (uuid,userpwd,reg_dt,mobile,m_auth,muuid,mobile_checked) values('".$uuid."','".$userpwd."',now(),'".$mobile."','".$m_auth."','".$muuid."','1')";
			$db->query($sql);
			$addrs_id=mysql_insert_id(); 
			//手机认证通过，送额度
			if ($m_auth == 1){
				$sql = "insert into user_glftcredit(UID,glftlevel,credit,senddt,isEnabled,rundt) value('".$addrs_id."',1,100,now(),1,now())";
				$db->query($sql);
			}
			$sql = "update user_imsi set imsi='".$imsi."' where uid='".$addrs_id."'";
			$db->query($sql);
			$success = 1;
			$rs=array("success"=>$success,"uuid"=>$muuid);
		}
	}else{
		$rs=array("success"=>"0","errCode"=>"1");
	}
}else{
	$rs=array("success"=>"0","errCode"=>"-1");
}

echo json_encode($rs);
?>
